$(function() {

	let requestUrl = 'https://www.yi-p.com/';
	let tipPanel = $('#tipPanel');
	let modal = $('#modal');
	let modalContent = $('#modalContent');
	let modalBtnGroup = $('#modalBtnGroup');
	let modalBackground = $('#modalBackground');
	let modalBtnFuns = {};
	
	let windowResize = function() {
		let width = window.innerWidth;
		let height = window.innerHeight;
		tipPanel.css({
			left: (width / 2 - 65) + 'px',
			top: (height / 2 - 150) + 'px'
		});
		modal.css({
			left: (width / 2 - 175) + 'px',
			top: (height / 2 - 150) + 'px'
		});
	}

	$(window).resize(function() {
		windowResize();
	});

	let ajax = function(url, dataType, data, successFun, errorFun, timeoutFun, timeout, type) {
		dataType = dataType || 'json';
		timeout = timeout || 15000;
		type = type || 'POST';
		$.ajax({
			type: type,
			url: url,
			data: data,
			dataType: dataType,
			timeout: timeout,
			success: successFun,
			error: function(XHR, textStatus, errorThrown) {
				if(textStatus == 'timeout' && timeoutFun) {
					timeoutFun();
				}
				else {
					errorFun(XHR, textStatus, errorThrown);
				}
			}
		});
	}

	let checkLogin = function(callback) {
		let session = window.localStorage.getItem('session');
		ajax(requestUrl + 'user/checkAdminLogin', 'json', {
			session: session
		}, function(data) {
			console.log(data);
			callback();
		}, function() {
			console.log('失败');
			callback();
		});
	}

	let showTip = function(params) {
		let {icon, content, time} = params;
		let iconList = {
			success: './success.png',
			error: './error.png',
			loading: './loading.png'
		};
		tipPanel.children('#tipImg').attr({src: iconList[icon] || iconList['success']});
		tipPanel.children('#tipContent').html(content);
		tipPanel.fadeIn(300, function() {
			if(icon == 'loading') {
				return;
			}
			setTimeout(function() {
				tipPanel.fadeOut(300);
			}, time || 1000);
		});
	}

	let hideTip = function() {
		tipPanel.fadeOut(300);
	}

	let showModal = function(params) {
		let {content, btnList} = params;
		let btn, funId;
		modalContent.html(content);
		for(let btnIndex in btnList) {
			btn = $('<span></span>');
			btn.addClass('modalBtn ' + btnList[btnIndex].type + 'Btn');
			funId = btnIndex;
			modalBtnFuns[funId] = btnList[btnIndex].fun;
			btn.data({'funId': funId});
			btn.html(btnList[btnIndex].name);
			modalBtnGroup.append(btn);
		}
		modal.fadeIn(300);
		modalBackground.fadeIn(300);
	}

	let hideModal = function() {
		modal.fadeOut(300);
		modalBackground.fadeOut(300);
	}

	let getWithdrawCashList = function() {
		ajax(requestUrl + 'withdrawCash/find', 'json', {
			storeId: 'huarenf',
			_3rdSession: window.localStorage.getItem('session')
		}, function(data) {
			console.log(data);
		}, function(XHR, textStatus, errorThrown) {
			console.log(textStatus);
		}, function() {
			console.log('timeout');
		});
	}

	$('#optionGroup').on('click', '.option', function(e) {
		let {panelid} = e.currentTarget.dataset;
		$('.panel').hide(150, function() {
			setTimeout(function() {
				$('#' + panelid).show(150);
			}, 100);
		});
	});

	$('#modalBtnGroup').on('click', '.modalBtn', function() {
		let funId = $(this).data('funId') + '';
		console.log(modalBtnFuns[funId]);
		if(modalBtnFuns[funId]) {
			modalBtnFuns[funId]();
		}
	});

	windowResize();

	checkLogin(function() {
		getWithdrawCashList();
	});

	// showTip({
	// 	icon: 'loading',
	// 	content: '请稍候'
	// });

	// showModal({
	// 	content: '确认删除？',
	// 	btnList: {
	// 		no: {
	// 			type: 'cancel',
	// 			name: '取消',
	// 			fun: function() {
	// 				hideModal();
	// 			}
	// 		},
	// 		yes: {
	// 			type: 'success',
	// 			name: '确认',
	// 			fun: function() {
	// 				alert();
	// 			}
	// 		}
	// 	}
	// });

});